from rest_framework.generics import ListAPIView, GenericAPIView,mixins
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import SPU, SPUSpecification
from meiduo_admin.serializers.spus import SPUSimpleSerializer, SPUSpecSerializer


class SPUSimpleView(ListAPIView):
    permission_classes = [IsAdminUser]

    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer

    pagination_class = None


# API-Generic-Mixin-List
# GET meiduo_admin/goods/(?P<pk>\d+)/specs/

class SPUSpecView(ListAPIView):
    permission_classes = [IsAdminUser]

    serializer_class = SPUSpecSerializer

    def get_queryset(self):
        # self.kwargs:字典,保存从url地址中提取所有的参数
        pk = self.kwargs['pk']
        return SPUSpecification.objects.filter(spu_id=pk)

    # def get(self, request, pk):
    #     return self.list(request,pk)

    pagination_class = None
    # def get(self,request,pk):
    #     """
    #     pk: SPU id
    #     获取SPU规格及数据
    #     1.根据pk获取所有数据
    #     2.序列化数据返回
    #     :param request:
    #     :param pk:
    #     :return:
    #     """
    #     # 1.根据pk获取所有数据
    #     specs = self.get_queryset()
    #     # 2. 序列化数据返回
    #     serializer = SPUSpecSerializer(specs, many=True)
    #     return Response(serializer.data)